<?php
namespace app\service;
use alipay\AopClient;  //查询物业token
use alipay\AlipaySystemOauthTokenRequest;//换取授权访问令牌
use alipay\AlipayTradeCreateRequest;
use stdClass;

class alipayService{
    const XCXPRIKEYPATH = '/www/wwwroot/parking_tp6/keypath/ydyqPrikey.txt';//私钥路径
    const XCXPUBKEYPATH = '/www/wwwroot/parking_tp6/keypath/ydyqPublicKey.txt';//支付宝公钥路径



    private $app_id;//支付宝分配给开发者的应用
    private $method;//接口名称
    private $format;//仅支持JSON  no
    private $charset = 'UTF-8';//请求使用的编码格式，如utf-8,gbk,gb2312等
    private $sign_type = 'RSA2';//商户生成签名字符串所使用的签名算法类型，目前支持RSA2和RSA，推荐使用RSA2
    private $sign;//商户请求参数的签名串  no
    private $timestamp;//发送请求的时间，格式"yyyy-MM-dd HH:mm:ss"
    private $version = '1.0';//调用的接口版本，固定为：1.0
    private $app_auth_token;//详见应用授权概述 no
    private $biz_content;//


    //XIOA
    public function xcxgetPublicContent(){
        $aop = new AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = '2021001163687196';
        $aop->rsaPrivateKey = file_get_contents(self::XCXPRIKEYPATH);//私钥路径
        $aop->alipayrsaPublicKey= file_get_contents(self::XCXPUBKEYPATH);//支付宝公钥路径
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        return $aop;
    }

    //小程序换取授权访问令牌
    public function xcxoauthToken($code){
        $aop = $this->xcxgetPublicContent();
        $request = new AlipaySystemOauthTokenRequest ();
        $request->setGrantType("authorization_code");
        $request->setCode($code);
        $request->setRefreshToken('');
        $result = $aop->execute ($request);
        if(property_exists($result,'error_response')){
            $responseNode = 'error_response';
        }else{
            $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        }
        return $result->$responseNode;
    }
    public function create($data)
    {
        $data = json_decode($data);

        $aop = $this->xcxgetPublicContent();
        $request = new AlipayTradeCreateRequest ();
        $object = new stdClass();
        $object->out_trade_no = $data->out_trade_no;
        $object->total_amount = $data->total_amount;
        $object->subject = $data->subject;
        $object->buyer_id = $data->buyer_id;
//        $object->sell_id=$data->merchant_id;
         $object->op_app_id="2021001163687196";
        //二级商户编号
//        $subMerchant = [
//            'merchant_id' => $data->merchant_id,
//        ];
//        $object->sub_merchant = $subMerchant;
        $object->product_code = 'JSAPI_PAY';//   JSAPI_PAY FACE_TO_FACE_PAYMENT
        $json = json_encode($object);

        //Log::write($json);
        $request->setBizContent($json);
        $request->setNotifyUrl($data->notifyUrl);
        $token = $data->token;
        $result = $aop->execute ($request, null, $token);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        return $result->$responseNode;
    }

}